#include "wuSolidShape.h"

wuSolidShape::wuSolidShape()
{

}

wuSolidShape::~wuSolidShape()
{
}


void wuSolidShape::setup(ofxBox2d * _box2d, ofRectangle rect, vector <ofPoint> *simpleCountour)
{
	// solidShape without skin
	wuBase2d::setup(_box2d, rect);

	for(int j=(*simpleCountour).size()/2; j<(*simpleCountour).size(); j++) {
		polyLine.addVertex((*simpleCountour)[j].x, (*simpleCountour)[j].y);
	}
	for(int j=0; j<(*simpleCountour).size()/2; j++) {
		polyLine.addVertex((*simpleCountour)[j].x, (*simpleCountour)[j].y);
	}

	// make the shape
	polyLine.simplify(20.0);
	polyLine.setPhysics(0.0, 0.5, 0.5);
	polyLine.create(box2d->getWorld());

}

// avoid inside rectangle-selection
void wuSolidShape::setup(ofxBox2d * _box2d, ofRectangle rect, vector <ofPoint> *simpleCountour, ofRectangle selection)
{
    int num_v=0;
	// solidShape without skin
	wuBase2d::setup(_box2d, rect);

	for(int j=(*simpleCountour).size()/2; j<(*simpleCountour).size(); j++) {
	    if(!((*simpleCountour)[j].x > selection.x && (*simpleCountour)[j].x < selection.x + selection.width && (*simpleCountour)[j].y > selection.y && (*simpleCountour)[j].y < selection.y + selection.height ))
	    {
	        polyLine.addVertex((*simpleCountour)[j].x, (*simpleCountour)[j].y);
	        num_v++;
	    }
	}
	for(int j=0; j<(*simpleCountour).size()/2; j++) {
	    if(!((*simpleCountour)[j].x > selection.x && (*simpleCountour)[j].x < selection.x + selection.width && (*simpleCountour)[j].y > selection.y && (*simpleCountour)[j].y < selection.y + selection.height ))
	    {
            polyLine.addVertex((*simpleCountour)[j].x, (*simpleCountour)[j].y);
	        num_v++;
	    }
	}

    if(num_v>3)
    {
        // make the shape
        polyLine.simplify(20.0);
        polyLine.setPhysics(0.0, 0.5, 0.5);
        polyLine.create(box2d->getWorld());
    }
}

void wuSolidShape::setup(ofxBox2d * _box2d, ofImage *_skin, ofRectangle rect, vector <ofPoint> *simpleCountour)
{
	// solidShape with skin
	wuBase2d::setup(_box2d, rect);
	wuBaseSkin::setSkin(_skin, rect);

	for(int j=(*simpleCountour).size()/2; j<(*simpleCountour).size(); j++) {
		polyLine.addVertex((*simpleCountour)[j].x, (*simpleCountour)[j].y);
	}
	for(int j=0; j<(*simpleCountour).size()/2; j++) {
		polyLine.addVertex((*simpleCountour)[j].x, (*simpleCountour)[j].y);
	}

	// make the shape
	polyLine.simplify(20.0);
	polyLine.setPhysics(0.0, 0.5, 0.5);
	polyLine.create(box2d->getWorld());

}

void wuSolidShape::draw(bool bDrawlines, int offX)
{
	wuBaseSkin::draw(offX);

	if(bDrawlines && offX==0)
    {
        ofSetColor(0,255,255);
		polyLine.draw();
    }
}

void wuSolidShape::clear()
{
	if(polyLine.isBody()) {
		polyLine.clear();
		polyLine.destroy();
	}

//	lineStrip.clear();
}

